# Contributor: nibon7 <nibon7@163.com>
# Maintainer: nibon7 <nibon7@163.com>
pkgname=v2ray
pkgver=5.14.1
pkgrel=0
pkgdesc="A platform for building proxies to bypass network restrictions"
url="https://v2fly.org"
arch="all"
license="MIT"
makedepends="go"
subpackages="$pkgname-openrc"
_geosite_ver=20240331083526
_geoip_ver=202403280038
source="$pkgname-core-$pkgver.tar.gz::https://github.com/v2fly/v2ray-core/archive/v$pkgver.tar.gz
	geosite-$_geosite_ver.dat::https://github.com/v2fly/domain-list-community/releases/download/$_geosite_ver/dlc.dat
	geoip-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip.dat
	geoip-only-cn-private-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip-only-cn-private.dat
	v2ray.initd
	v2ray.confd
	"
options="net" # test FakeDNS

builddir="$srcdir/$pkgname-core-$pkgver"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	local ldflags="
		-X github.com/v2fly/v2ray-core/v5.codename=$pkgname
		-X github.com/v2fly/v2ray-core/v5.version=$pkgver
		-X github.com/v2fly/v2ray-core/v5.build=$SOURCE_DATE_EPOCH
		-buildid=
		"
	go build -trimpath -ldflags "$ldflags" -o v2ray ./main
}

check() {
	mkdir -p testing/temp
	cp "$srcdir"/geosite-$_geosite_ver.dat testing/temp/geosite.dat
	cp "$srcdir"/geoip-$_geoip_ver.dat testing/temp/geoip.dat

	# due to heavy CI workload, scenario tests always timeout
	local pkgs=$(go list ./... | grep -v \
		-e 'v5/testing/scenarios$' \
	)
	go test $pkgs
}

package() {
	install -Dm755 -d "$pkgdir"/etc/$pkgname
	install -m644 release/config/*.json -t "$pkgdir"/etc/$pkgname

	install -Dm755 -d "$pkgdir"/usr/share/$pkgname
	install -m644 "$srcdir"/geosite-$_geosite_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geosite.dat
	install -m644 "$srcdir"/geoip-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip.dat
	install -m644 "$srcdir"/geoip-only-cn-private-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip-only-cn-private.dat

	install -Dm755 v2ray "$pkgdir"/usr/bin/v2ray

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
}

sha512sums="
0090d2aa50b3a02d673a1c656078b62ceb8ddba1b984f94b66aa2cf5341c8c97f032f1c85ee3583a06d1c55864aa7fd237632e2c53be4c73a96c3ac260f92ade  v2ray-core-5.14.1.tar.gz
c315f56ea7308dea2675b80a9ea398c8c76b62b27f312c6610de5497e6863647b1189efd3eb56c774ddf24ffb32957cc41c24ed538e13f7cc2634eafd923c921  geosite-20240331083526.dat
cd0594fa4c5791f98052721e4ba8898a5d76d67e7bd0d2f1f23b8800ff26fde8235a990d50d2a5e6be60c8b2d054cfc0a3b00ff13a2e2742dcee6862f9618493  geoip-202403280038.dat
6e20b674b084960e3f0220d8e1e21dac3342db516080899eb161c469827412cf8eee8af85700a0060c481c5381631ce6efb2f101490ea29e014ee60dca9205f9  geoip-only-cn-private-202403280038.dat
714e9f93efb74a2b30a74df95fa2bdaa854188d5fce3ed4514f2d19e790baf31dc78c18726700013113501dd31981a2e0582150b64d7235121242c01e2e5b8d9  v2ray.initd
4b00bfe001780b9ac7eff1b9776a98ae5e7050362ebfb0ed2550f8f1244e9474d186dcde5bccba01dbdd908e9a932f75238becae6585efe674ebc5ffb8cbf0a6  v2ray.confd
"
